package com.zhazha.earlybird.dao;

import com.zhazha.earlybird.beans.UserDetailInfo;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

public class UserDetailDAO {

	private SQLiteDatabase mDatabase;
	private static final String USER_DETAIL_TABLE = "user_detail";
	
	private static final String USER_ID 				= "id";
	private static final String USER_MAJOR				= "major_id";
	private static final String USER_DEGREE				= "degree_id";
	private static final String USER_HOMETOWN			= "hometown_id";
	private static final String USER_UNIVERSITY 		= "university_id";
	private static final String USER_UNIVERSITY_CITY	= "university_city_id";
	private static final String USER_GRADUATE_DATE		= "graduate_date";
	
	private static final int USER_ID_INDEX 				= 0;
	private static final int USER_MAJOR_INDEX			= 1;
	private static final int USER_DEGREE_INDEX			= 2;
	private static final int USER_HOMETOWN_INDEX		= 3;
	private static final int USER_UNIVERSITY_INDEX 		= 4;
	private static final int USER_UNIVERSITY_CITY_INDEX	= 5;
	private static final int USER_GRADUATE_DATE_INDEX	= 6;
	
	public UserDetailDAO(SQLiteDatabase db) {
		mDatabase = db;
	}
	
	
	public boolean insert(UserDetailInfo user) {
		
		try {
			ContentValues cv = new ContentValues();
			cv.put(USER_ID, 			user.getId());
			cv.put(USER_MAJOR, 		user.getMajor());
			cv.put(USER_DEGREE, 		user.getDegree());
			cv.put(USER_HOMETOWN, 		user.getHometown());
			cv.put(USER_UNIVERSITY,	user.getUniversity());
			cv.put(USER_UNIVERSITY_CITY, user.getUniversityCity());
			cv.put(USER_GRADUATE_DATE, user.getGraduateDate());
			
			this.mDatabase.insert(USER_DETAIL_TABLE, null, cv);
			return true;
		}catch (Exception e) {
			e.printStackTrace();
			return false;
		}
		
	}
	
	public boolean delete(int id) {
		try {
			String whereClause = "id=?";
			String[] whereArgs = {String.valueOf(id)};
			this.mDatabase.delete(USER_DETAIL_TABLE, whereClause, whereArgs);
			return true;
		}catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}
	
	public boolean update(int id, ContentValues cv) {
		try {
			String whereClause = "id=?";
			String[] whereArgs = {String.valueOf(id)};
			this.mDatabase.update(USER_DETAIL_TABLE, cv, whereClause, whereArgs);
			return true;
		}catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}
	
	public UserDetailInfo queryById(int id) {
		UserDetailInfo user = null;
		
		try {
			String selection = "id=?";
			String[] selectionArgs = {String.valueOf(id)};
			Cursor c = this.mDatabase.query(USER_DETAIL_TABLE, null, selection, selectionArgs, null, null, null);
			
			if(c.moveToFirst()) {
				user = new UserDetailInfo();
				user.setId(c.getInt(USER_ID_INDEX));
				user.setMajor(c.getInt(USER_MAJOR_INDEX));
				user.setDegree(c.getInt(USER_DEGREE_INDEX));
				user.setHometown(c.getInt(USER_HOMETOWN_INDEX));
				user.setGraduateDate(c.getString(USER_GRADUATE_DATE_INDEX));
				user.setUniversity(c.getInt(USER_UNIVERSITY_INDEX));
				user.setUniversityCity(c.getInt(USER_UNIVERSITY_CITY_INDEX));
			}
			
			return user;
			
		}catch (Exception e) {
			e.printStackTrace();
			return user;
		}
	}
}
